package us.wili.dev.common.model.validator;

import javax.validation.ConstraintValidator;
import javax.validation.ConstraintValidatorContext;
import java.util.regex.Pattern;

/**
 * Created by JasonY on 17/2/19.
 */
public class PhoneValidator implements ConstraintValidator<Phone, Object> {


    private final static String PHONE_REG = "^((13[0-9])|(14[5,7])|(15[0-3,5-9])|(17[0,3,5-8])|(18[0-9])|166|198|199|(147))\\\\\\\\d{8}$";

    private Pattern phonePattern = Pattern.compile(PHONE_REG);

    @Override
    public void initialize(Phone phone) {

    }

    @Override
    public boolean isValid(Object value, ConstraintValidatorContext context) {
        if (value == null) {
            return true;
        }

        if (!(value instanceof String)) {
            return false;
        }

        return phonePattern.matcher(value.toString()).matches();
    }
}
